package com.idanchuang.component.provider.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yjy
 * @date 2019/11/21 9:06
 **/
public class RpcConfig implements ApplicationContextAware {

    /** 接口实现类的后缀 */
    private List<String> suffixList;
    /** 是否启用签名校验 */
    private boolean enabledSign;
    /** 签名key */
    private String signKey;
    /** 接受的接口访问密钥 */
    private List<String> secretKeyList;
    /** 扩展请求头 */
    private Map<String, String> headers;
    /** 本地变量表数据在RPC请求中传递时的HeaderName */
    private String threadLocalHeaderName;
    /** 请求来源参数名 */
    private String rpcFromName;
    /** 拦截器异常时是否通过 */
    private boolean passIfError;

    private static RpcConfig instance;
    private static ApplicationContext applicationContext;

    {

        suffixList = new ArrayList<>();
        suffixList.add("ClientImpl");
        suffixList.add("Client");

        enabledSign = true;

        signKey = "ABM-RPC-SIGN";
        secretKeyList = new ArrayList<>();
        secretKeyList.add("123456");

        threadLocalHeaderName = "ABM-RPC-THREAD-LOCAL";
        headers = new HashMap<>();

        rpcFromName = "rpc_from";
        passIfError = true;
    }

    public List<String> getSuffixList() {
        return suffixList;
    }

    public void setSuffixList(List<String> suffixList) {
        this.suffixList = suffixList;
    }

    public String getSignKey() {
        return signKey;
    }

    public void setSignKey(String signKey) {
        this.signKey = signKey;
    }

    public List<String> getSecretKeyList() {
        return secretKeyList;
    }

    public boolean isPassIfError() {
        return passIfError;
    }

    public void setPassIfError(boolean passIfError) {
        this.passIfError = passIfError;
    }

    public static void setInstance(RpcConfig instance) {
        RpcConfig.instance = instance;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public void setSecretKeyList(List<String> secretKeyList) {
        this.secretKeyList = secretKeyList;
    }

    public String getThreadLocalHeaderName() {
        return threadLocalHeaderName;
    }

    public boolean isEnabledSign() {
        return enabledSign;
    }

    public void setEnabledSign(boolean enabledSign) {
        this.enabledSign = enabledSign;
    }

    public void setThreadLocalHeaderName(String threadLocalHeaderName) {
        this.threadLocalHeaderName = threadLocalHeaderName;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    public String getRpcFromName() {
        return rpcFromName;
    }

    public void setRpcFromName(String rpcFromName) {
        this.rpcFromName = rpcFromName;
    }

    public static RpcConfig getInstance() {
        if (instance == null) {
            synchronized (RpcConfig.class) {
                if (instance == null) {
                    instance = applicationContext.getBean(RpcConfig.class);
                }
            }
        }
        return instance;
    }

    @Override
    public String toString() {
        return "RpcConfig{" +
                "suffixList=" + suffixList +
                ", enabledSign=" + enabledSign +
                ", signKey='" + signKey + '\'' +
                ", secretKeyList=" + secretKeyList +
                ", headers=" + headers +
                ", threadLocalHeaderName='" + threadLocalHeaderName + '\'' +
                ", rpcFromName='" + rpcFromName + '\'' +
                ", passIfError=" + passIfError +
                '}';
    }

    public void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        RpcConfig.applicationContext = applicationContext;
    }
}
